local root "N:/Project/50732_RDRC/DC1/~51702/BC23-TBD (SSI Medicaid)/Data/Output/"

// Start with population data, collapse to state level
use "`root'/pop_data_grp.dta", clear

gen st = floor(fips/1000)
collapse (sum) pop_2019, by(st)

statastates, fips(st) nogen
rename st state_fips

tempfile ssi_data
save "`ssi_data'", replace

// Only keep the "good states" that we have models for
import excel "`root'/SSI Status Verification/good_states.xlsx", first clear
statastates, ab(state) nogen
keep good_state state_fips
merge 1:1 state_fips using "`ssi_data'", nogen assert(match)

save "`ssi_data'", replace

// Merge on the number of child SSI recipients in each state
import excel using "`root'/SSI Status Verification/_00_ssi_asr_counts.xlsx", first clear
statastates, name(State) nogen
ren B ssi_2017
ren C ssi_2018 
ren D ssi_2019
keep ssi_* state_fips

merge 1:1 state_fips using "`ssi_data'", nogen assert(match)

// Calculate some totals across the good states
by good_state, sort: egen tot_ssi_2017 = total(ssi_2017)
by good_state: egen tot_ssi_2018 = total(ssi_2018)
by good_state: egen tot_ssi_2019 = total(ssi_2019)

drop tot_ssi_* ssi_2017 ssi_2018 

// Key variable: child SSI recipients per 1,000 children
gen ssi_percap = 1000 * ssi_2019 / pop_2019
ren state_abbrev state 

keep state ssi_percap
save "`ssi_data'", replace

// Get the vignette analysis output created, merge with the child SSI participation
import excel using "`root'/vignette_analysis/_05_synthetic_profiles_prob_ssi_clean.xlsx", first clear

ren *, l

merge 1:1 state using "`ssi_data'"
keep if _merge == 3
drop _merge

label variable ssi_percap "Child SSI participation per 1,000 children"

local graph_combine

// Create scatter plots, then combine into a single graph
tw (sc othdd ssi_percap, mlabel(state) msymbol(i)) (lfit othdd ssi_percap), scheme(s1mono) ylabel(0(0.2)0.6) ytitle("Predicted probability of SSI receipt") legend(off) title("Other developmental disabilities")
graph save "`root'/summary_graphs/graphs/othdd.gph", replace
local graph_combine "`graph_combine' `"`root'/summary_graphs/graphs/othdd"'"


tw (sc behavioraldisorder ssi_percap, mlabel(state) msymbol(i)) (lfit behavioraldisorder ssi_percap), scheme(s1mono) ylabel(0(0.2)0.6) ytitle("Predicted probability of SSI receipt") legend(off) title("Behavioral disorder")
graph save "`root'/summary_graphs/graphs/behav.gph", replace
local graph_combine "`graph_combine' `"`root'/summary_graphs/graphs/behav"'"

tw (sc adhd ssi_percap, mlabel(state) msymbol(i)) (lfit adhd ssi_percap), scheme(s1mono) ylabel(0(0.2)0.6) ytitle("Predicted probability of SSI receipt") legend(off) title("ADHD")
graph save "`root'/summary_graphs/graphs/adhd.gph", replace
local graph_combine "`graph_combine' `"`root'/summary_graphs/graphs/adhd"'"


tw (sc cerebralpalsy ssi_percap, mlabel(state) msymbol(i)) (lfit cerebralpalsy ssi_percap), scheme(s1mono) ylabel(0(0.2)0.6) ytitle("Predicted probability of SSI receipt") legend(off) title("Cerebral palsy")
graph save "`root'/summary_graphs/graphs/cp.gph", replace
local graph_combine "`graph_combine' `"`root'/summary_graphs/graphs/cp"'"

graph combine `graph_combine', rows(2) cols(2) scheme(s1mono) iscale(0.75) xsize(9) ysize(6)
graph export "`root'/summary_graphs/vignettes.png", replace hei(1200)

reg adhd ssi_percap, r
reg othdd ssi_percap, r
reg cerebralpalsy ssi_percap, r
reg behavioraldisorder ssi_percap, r

